package org.fungene.util.chart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class ChartUtil {

	private DefaultCategoryDataset dataset;

	private JFreeChart drawBarChart() {

		JFreeChart chart = ChartFactory.createBarChart("", "Probe Set ID",
				"Value", dataset, PlotOrientation.VERTICAL, true, true, true);

		CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
		CategoryAxis categoryaxis = categoryplot.getDomainAxis();
		categoryaxis.setCategoryLabelPositions(CategoryLabelPositions
				.createUpRotationLabelPositions(0.5 * 3.1415926535897931D));
		return chart;
	}

	private JFreeChart drawLineChart() {

		JFreeChart chart = ChartFactory.createLineChart("", "Probe Set ID",
				"Value", dataset, PlotOrientation.VERTICAL, true, false, false);

		CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
		CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
		categoryitemrenderer
				.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		categoryitemrenderer.setSeriesItemLabelsVisible(0, Boolean.TRUE);

		return chart;
	}

	public ChartUtil(DefaultCategoryDataset dataset) {
		this.dataset = dataset;
	}

	public JFreeChart genByCmd(int cmdType) {
		JFreeChart chart = null;
		switch (cmdType) {
		case ChartConstant.CMD_BAR:
			return this.drawBarChart();
		case ChartConstant.CMD_LINE:
			return this.drawLineChart();
		}
		return chart;
	}
}
